package com.phpxiu.app.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.UserHomeContent;
import com.phpxiu.app.model.UserHomeInfo;
import com.phpxiu.app.model.UserHomeRank;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.RoomFansRank;
import com.phpxiu.app.view.UserHome;
import com.phpxiu.app.view.custom.FrescoImageView;
import com.phpxiu.app.view.custom.IFrescoImageView;

import java.util.List;

/**
 * Created by WuJinZhou on 2016/6/6.
 */
public class UserHomeFirstFragment extends BaseFragment implements View.OnClickListener {
    public final static String TAG="UserHomeFirstFragment";
    private final static int HANDLE_UPDATE_UI=0x234;//更新UI
    private UserHomeInfo mUser;
    private FrescoImageView userNo1,userNo2,userNo3;//票数贡献前三名
    private TextView ageView;
    private TextView addressView;
    private TextView jobView;
    private TextView introContentView;
    public static UserHomeFirstFragment builder(String arg){
        UserHomeFirstFragment fragment=new UserHomeFirstFragment();
        Bundle b = new Bundle();
        b.putString(ARG,arg);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.user_home_first_fragment,null);
        init();
        return rootView;
    }

    private void init(){
        rootView.findViewById(R.id.user_home_open_cost_rank_link).setOnClickListener(this);
        userNo1=(FrescoImageView)rootView.findViewById(R.id.user_no1_view);
        userNo2=(FrescoImageView)rootView.findViewById(R.id.user_no2_view);
        userNo3=(FrescoImageView)rootView.findViewById(R.id.user_no3_view);
        ageView=(TextView)rootView.findViewById(R.id.user_home_age_view);
        addressView=(TextView)rootView.findViewById(R.id.user_home_address_view);
        jobView=(TextView)rootView.findViewById(R.id.user_home_job_view);
        introContentView=(TextView)rootView.findViewById(R.id.user_home_intro_view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.user_home_open_cost_rank_link:
            case R.id.user_no1_view:
            case R.id.user_no2_view:
            case R.id.user_no3_view:
                if(mUser!=null) {
                    Intent rank = new Intent(getActivity(), RoomFansRank.class);
                    KKYUtil.log("总票数收入：" + mUser.getTotal_income() + "");
                    rank.putExtra(RoomFansRank.EXTRA_PARAM_USER_ID_KEY,mUser.getUid());
                    rank.putExtra(RoomFansRank.EXTRA_PARAM_USER_TICKETS_KEY,mUser.getTotal_income());
                    startActivity(rank);
                }
                //Intent home=new Intent(getActivity(), UserHome.class);
                //home.putExtra(UserHome.EXTRA_PARAM_KEY_UID,v.getTag().toString());
                //startActivity(home);
                break;
            default:
                break;
        }
    }

    @Override
    public void handleMsg(Message msg) {
        super.handleMsg(msg);
        switch (msg.what){
            case HANDLE_UPDATE_UI:
                refreshContent((UserHomeContent)msg.obj);
                break;
            default:
                break;
        }
    }

    public void update(UserHomeContent content){
        Message msg=handler.obtainMessage();
        msg.what=HANDLE_UPDATE_UI;
        msg.obj=content;
        handler.sendMessage(msg);
    }

    /**
     * 刷新内容
     */
    private void refreshContent(UserHomeContent content){
        mUser=content.getInfo();
        List<UserHomeRank> top=content.getRank();
        if(top!=null&&top.size()>0){
            switch (top.size()){
                case 1:
                    userNo1.setImageURI(Uri.parse(top.get(0).getAvatar()));
                    userNo1.setTag(top.get(0).getUid());
                    //userNo1.setOnClickListener(this);
                    //userNo2.setOnClickListener(null);
                    //userNo3.setOnClickListener(null);
                    break;
                case 2:
                    userNo1.setImageURI(Uri.parse(top.get(0).getAvatar()));
                    userNo1.setTag(top.get(0).getUid());
                    //userNo1.setOnClickListener(this);
                    userNo2.setImageURI(Uri.parse(top.get(1).getAvatar()));
                    userNo2.setTag(top.get(1).getUid());
                    //userNo2.setOnClickListener(this);
                    //userNo3.setOnClickListener(null);;
                    break;
                case 3:
                    userNo1.setImageURI(Uri.parse(top.get(0).getAvatar()));
                    userNo1.setTag(top.get(0).getUid());
                    //userNo1.setOnClickListener(this);
                    userNo2.setImageURI(Uri.parse(top.get(1).getAvatar()));
                    userNo2.setTag(top.get(1).getUid());
                    //userNo2.setOnClickListener(this);
                    userNo3.setImageURI(Uri.parse(top.get(2).getAvatar()));
                    userNo3.setTag(top.get(2).getUid());
                    //userNo3.setOnClickListener(this);
                    break;
            }
        }else{
            //userNo1.setOnClickListener(null);
            //userNo2.setOnClickListener(null);
            //userNo3.setOnClickListener(null);
        }
        ageView.setText(mUser.getBirthday());
        addressView.setText(mUser.getAddress());
        jobView.setText(mUser.getJob());
        String intro=mUser.getIntro();
        intro=(intro==null||"".equals(intro))?getString(R.string.default_intro_content):intro;
        introContentView.setText(intro);
    }
}
